//
//  imageUploadTool.m
//  imgeUpDemo
//
//  Created by 方伟 on 16/5/31.
//  Copyright © 2016年 方伟. All rights reserved.
//

#import "imageUploadTool.h"
#import "NSStringchange.h"



@implementation imageUploadTool


-(void)uplodImage{


    
//    [GiabHUD showUIBlockingIndicatorWithText:@"正在申请..."];
    
        [UPYUNConfig sharedInstance].DEFAULT_BUCKET = spaceUpYun_work;
        [UPYUNConfig sharedInstance].DEFAULT_PASSCODE = upYun_workPassword;
        __block UpYun *uy = [[UpYun alloc] init];
    
    
    
    /**
     *	@brief	根据 UIImage 上传
     */
    
    if (_selectedImage.size.width  >1200) {
        
        _selectedImage = [_selectedImage scaleToSize:_selectedImage size:CGSizeMake(1200, 80)];
    }

    NSData *imageData = UIImageJPEGRepresentation(_selectedImage, 0.6);
    
    _selectedImage = [UIImage imageWithData:imageData];
    
    
    NSString *path = [NSStringchange uploadTypeWith:_type];
    
    if (!_index) {
        _index = 0;
    }
    
    NSString *key =  [self getSaveKeyWith:@"jpg" withPath:path withUserID:_worker_id withIndex:_index ];
    NSLog(@"%@",key);
    
    [uy uploadImage:_selectedImage savekey:key];
    
    
    WS;
    uy.successBlocker = ^(NSURLResponse *response, id responseData) {
            
            
            if ([responseData[@"code"] intValue]==200) {
                
                NSString * url = [NSString stringWithFormat:@"%@%@",BaseUpYunUrl_worker,responseData[@"url"]];
                NSLog(@"%@",url);
                
                if ([weakSelf.delegate respondsToSelector:@selector(returnImageURL:)]) {

                    [weakSelf.delegate returnImageURL:url];
                }
               
            
                
            }
            else{
                if ([weakSelf.delegate respondsToSelector:@selector(returnError:)]) {
                    [weakSelf.delegate returnError:@"上传图片异常"];
                }
//                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"上传图片异常" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//                [alert show];
            }
        };
        
    
    uy.failBlocker = ^(NSError * error) {
        
        
//            NSString *message = [error.userInfo objectForKey:@"message"];
        if ([self.delegate respondsToSelector:@selector(returnError:)]) {
            [self.delegate returnError:@"上传图片失败"];
        }
        };
    
    
    

        
    
}



-(void)_sendVerifyRequest{

    
    NSLog(@"网络请求");
    
    
}
//上传图片到upYun
- (NSString * )getSaveKeyWith:(NSString *)suffix withPath:(NSString*)path withUserID:(NSString *)useID withIndex:(int)index{
    /**
     *	@brief	方式1 由开发者生成saveKey
     */
    return [NSString stringWithFormat:@"/%@/%@/%@_%d.%@", path,useID,[self getDateString] ,index,suffix];
    /**
     *	@brief	方式2 由服务器生成saveKey
     */
    //    return [NSString stringWithFormat:@"/{year}/{mon}/{filename}{.suffix}"];
    /**
     *	@brief	更多方式 参阅 http://docs.upyun.com/api/form_api/#_4
     */
}

- (NSString *)getDateString {
    NSDate *curDate = [NSDate date];//获取当前日期
    //    NSDateFormatter *formater = [[ NSDateFormatter alloc] init];
    //    [formater setDateFormat:@"yyyy/MM/dd"];//这里去掉 具体时间 保留日期
    //    NSString * curTime = [formater stringFromDate:curDate];
    NSString * curTime = [NSString stringWithFormat:@"%.0f" ,[curDate timeIntervalSince1970]];
    return curTime;
}

@end
